package com.usooft.metaTalent.core.wx.service.basic;

import com.usooft.metaTalent.core.wx.entity.WxOpenUser;
import com.usooft.metaTalent.core.wx.query.WxOpenUserQuery;
import com.usooft.metaTalent.core.wx.service.WxOpenUserService;
import com.usooft.metaTalent.core.wx.service.base.BaseWxOpenUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static com.usooft.framework.core.utils.Checks.checkNotNull;

@Service
@RequiredArgsConstructor
public class BasicWxOpenUserService extends BaseWxOpenUserService implements WxOpenUserService {

    @Override
    public Optional<WxOpenUser> findByUnionId(String unionId) {
        checkNotNull(unionId, "必须提供 UnionID");
        return findOne(WxOpenUserQuery.Filter.builder()
                .unionIdIs(unionId)
                .build())
                .map(v -> v);
    }

    @Override
    @Transactional
    public WxOpenUser create(String unionId, String openId) {
        return internalAdd(openId, unionId);
    }

    @Override
    public void edit(WxOpenUser wxOpenUser) {
        this.internalUpdate(wxOpenUser);
    }

    // region 自动生成
    // endregion
}